#include "makecells.h"
#include "../geometry/grid.h"

cell_obj make_cells(map_obj const & objs, double cell_size)
{	
	cell_obj result;
	map_obj::const_iterator it = objs.begin();
	for(; it != objs.end(); ++it)
	{
		grid_t<> grid(it->second.polygon, cell_size);
		
		for(size_t i = 0; i < grid.size(); ++i)
		{
			if(!grid[i].empty())
			{
				std::pair<int, int> idx = std::make_pair((int)grid[i].center().x, (int)grid[i].center().y);
				cell_obj::iterator cell_it = result.find(idx); 
				if(cell_it == result.end())
				{
					CELL_DATA cell_data;
					cell_data.cell_id = idx;
					cell_data.objects.push_back(it->first);
					result.insert(std::make_pair(idx, cell_data));
				}
				else
				{
					cell_it->second.objects.push_back(it->first);
				}
			}
		}
	}
	return result;
}